//
//  XJRecommendGoodsCollectionViewCell.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/10.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJRecommendGoodsCollectionViewCell.h"

#import "RecommendGoodsModel.h"

#import "UIImage+ColorImage.h"
#import <UIImageView+WebCache.h>

@interface XJRecommendGoodsCollectionViewCell ()

@property (nonatomic, strong) UIImageView * rgGoodsImgV;//商品图片
@property (nonatomic, strong) UILabel * rgGoodsTitleLab;//商品名称
@property (nonatomic, strong) UILabel * rgGoodsPriceLab;//商品价格
@property (nonatomic, strong) UILabel * rgGoodsDiscardLab;//弃用价格

@end

@implementation XJRecommendGoodsCollectionViewCell

- (UIImageView *)rgGoodsImgV{
    if (!_rgGoodsImgV) {
        UIImageView * rgGoodsImgV = [UIImageView new];
        self.rgGoodsImgV = rgGoodsImgV;
        [self addSubview:rgGoodsImgV];
    }
    return _rgGoodsImgV;
}

- (UILabel *)rgGoodsTitleLab{
    if (!_rgGoodsTitleLab) {
        UILabel * rgGoodsTitleLab = [UILabel new];
        self.rgGoodsTitleLab = rgGoodsTitleLab;
        [self addSubview:rgGoodsTitleLab];
    }
    return _rgGoodsTitleLab;
}

- (UILabel *)rgGoodsPriceLab{
    if (!_rgGoodsPriceLab) {
        UILabel * rgGoodsPriceLab = [UILabel new];
        self.rgGoodsPriceLab = rgGoodsPriceLab;
        [self addSubview:rgGoodsPriceLab];
    }
    return _rgGoodsPriceLab;
}

- (UILabel *)rgGoodsDiscardLab{
    if (!_rgGoodsDiscardLab) {
        UILabel * rgGoodsDiscardLab = [UILabel new];
        self.rgGoodsDiscardLab = rgGoodsDiscardLab;
        [self addSubview:rgGoodsDiscardLab];
    }
    return _rgGoodsDiscardLab;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
        
        
        // 6-9
//        self.backgroundColor = [UIColor redColor];
    }
    return self;
    
}

- (void)setupSubviews{
    
    
    self.rgGoodsImgV.frame = CGRectMake(ADAPTWidth(4), 0, (KWidth - 4 )/2, (KWidth - 4 )/2);
    [self.rgGoodsImgV sd_setImageWithURL:[NSURL URLWithString:@"http://img.suneasecloud.com/uploadfilesapi/435/201801/22/201801220011.jpg"] placeholderImage:[UIImage imageWithColor:XJCOLOR_RGB(221, 221, 221)]];
    self.rgGoodsImgV.contentMode =  UIViewContentModeScaleAspectFill;
    self.rgGoodsImgV.layer.masksToBounds = YES;
    self.rgGoodsImgV.layer.cornerRadius = 10.0;
    self.rgGoodsImgV.sd_layout
    .centerXEqualToView(self)
    .topEqualToView(self)
    .widthIs(ADAPTWidth(175))
    .heightIs(ADAPTHeight(175));
    

    NSString * titleString = @"商品名商品名商品名商品名商品名商品名商品名商品名";
    self.rgGoodsTitleLab.font = XJFONTSET_R(14);
    self.rgGoodsTitleLab.textColor = XJCOLOR_RGB(17, 17, 17);
    self.rgGoodsTitleLab.text = titleString;
    
    self.rgGoodsTitleLab.numberOfLines = 2;
    self.rgGoodsTitleLab.sd_layout
    .leftSpaceToView(self, ADAPTHeight(8))
    .topSpaceToView(self.rgGoodsImgV, 4)
    .widthIs(ADAPTWidth(175))
    .heightIs(50);

    self.rgGoodsPriceLab.font = XJFONTSET_M(16);
    self.rgGoodsPriceLab.textColor = XJCOLOR_RGB(244, 67, 54);
    self.rgGoodsPriceLab.text = @"￥99.00";
    CGFloat rgGoodsPriceLabW = [UILabel labelWithLabel:self.rgGoodsPriceLab getWidthWithTitle:self.rgGoodsPriceLab.text font:self.rgGoodsPriceLab.font];
    CGFloat rgGoodsPriceLabH = [UILabel labelWithLabel:self.rgGoodsPriceLab getHeightByWidth:rgGoodsPriceLabW title:self.rgGoodsPriceLab.text font:self.rgGoodsPriceLab.font];
    self.rgGoodsPriceLab.sd_layout
    .leftSpaceToView(self, ADAPTHeight(8))
    .topSpaceToView(self.rgGoodsTitleLab, 1.5)
    .widthIs(rgGoodsPriceLabW)
    .heightIs(rgGoodsPriceLabH);
    
    self.rgGoodsDiscardLab.font = XJFONTSET_R(14);
    self.rgGoodsDiscardLab.textColor = XJCOLOR_RGB(121, 130, 133);
    NSString * priceString = @"¥199.00";
    CGFloat rgGoodsDiscardLabW = [UILabel labelWithLabel:self.rgGoodsDiscardLab
                                       getWidthWithTitle:priceString
                                                    font:self.rgGoodsDiscardLab.font];
    
    CGFloat rgGoodsDiscardLabH = [UILabel labelWithLabel:self.rgGoodsDiscardLab
                                        getHeightByWidth:rgGoodsDiscardLabW
                                                   title:priceString
                                                    font:self.rgGoodsDiscardLab.font];
    self.rgGoodsDiscardLab.sd_layout
    .rightSpaceToView(self,ADAPTHeight(8))
    .topSpaceToView(self.rgGoodsTitleLab, 2)
    .widthIs(rgGoodsDiscardLabW)
    .heightIs(rgGoodsDiscardLabH);
    
    //添加中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:priceString attributes:attribtDic];
    // 赋值
    self.rgGoodsDiscardLab.attributedText = attribtStr;
}

#pragma mark - setter 方法 赋值
- (void)setRgModel:(RecommendGoodsModel *)rgModel{
    
    _rgModel = rgModel;
    
    
    NSURL *imgUrl = [NSURL URLWithString:rgModel.cover];
    [_rgGoodsImgV sd_setImageWithURL:imgUrl placeholderImage:[UIImage imageWithColor:XJCOLOR_RGB(221, 221, 221)]];
    _rgGoodsTitleLab.text = rgModel.name;
    _rgGoodsPriceLab.text = [NSString stringWithFormat:@"¥%.2f",[rgModel.price floatValue]];

    
    // 原价 高价
    NSString * market_price = [NSString stringWithFormat:@"¥%.2f",[rgModel.market_price floatValue]];
    
    //添加中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:market_price attributes:attribtDic];
    // 赋值
    self.rgGoodsDiscardLab.attributedText = attribtStr;
}




@end
